#include "gif.h"

#ifdef __cplusplus
extern "C"{
#endif
/**
* Global VM reference, initialized in JNI_OnLoad
*/
static JavaVM *g_jvm;
static struct JavaVMAttachArgs attachArgs = {.version=JNI_VERSION_1_6, .group=NULL, .name="GifIOThread"};
static ColorMapObject *defaultCmap;

JNIEnv *getEnv(void) {
    JNIEnv *env;
    if (g_jvm->AttachCurrentThread((void **)&env, &attachArgs) == JNI_OK) {
        return env;
    }
    return NULL;
}


void DetachCurrentThread(void) {
    g_jvm->DetachCurrentThread();
}

JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *vm, void * reserved) {
    g_jvm = vm;
    JNIEnv *env;
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        return JNI_ERR;
    }

    defaultCmap = GifMakeMapObject(8, NULL);
    if (defaultCmap != NULL) {
        uint_fast16_t iColor;
        for (iColor = 1; iColor < 256; iColor++) {
            defaultCmap->Colors[iColor].Red = (GifByteType) iColor;
            defaultCmap->Colors[iColor].Green = (GifByteType) iColor;
            defaultCmap->Colors[iColor].Blue = (GifByteType) iColor;
        }
    } else {
        throwException(env, OUT_OF_MEMORY_ERROR, OOME_MESSAGE);
    }

    struct timespec ts;
    if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1) {
        //sanity check here instead of on each clock_gettime() call
        throwException(env, RUNTIME_EXCEPTION_BARE, "CLOCK_MONOTONIC_RAW is not present");
    }
    return JNI_VERSION_1_6;
}

JNIEXPORT void JNICALL JNI_OnUnload(JavaVM * vm, void * reserved) {
    GifFreeMapObject(defaultCmap);
}

ColorMapObject *getDefColorMap(void) {
    return defaultCmap;
}

#ifdef __cplusplus
}
#endif